<?php


namespace App\Http\Model\Job;


use App\Enum\ClassListPayResultHistoryEnum;
use Illuminate\Support\Facades\DB;

class PfsHeadAddclassincrementModel
{

    /**
     * 查询指定监管账户信息
     *
     * @param $pfsId
     *
     * @return array|false|mixed
     */
    public static function pfs($pfsId)
    {
        $data = DB::select('SELECT * FROM `pfs_dw_ini` WHERE `id`=:id AND `is_del`=0', [
            'id' => $pfsId,
        ]);
        return empty($data) ? [] : current($data);
    }


    /**
     * 查询需要推送的记录
     *
     * @param $id
     *
     * @return array|false|mixed
     */
    public static function addClassIncrement($id)
    {
        $data = DB::select('SELECT * FROM `class_list_pay_result_history` WHERE `id`=:id', [
            'id' => $id,
        ]);
        return empty($data) ? [] : current($data);
    }


    /**
     * 同步结果入库
     *
     * @param $data
     * @param $result
     *
     * @return bool
     */
    public static function save($data, $result, $errMsg = '')
    {
        $list = [];
        foreach ($data['list'] as $val) {
            if (isset($result[$val->class_id]->success)) {
                $vResult = $result[$val->class_id]->success === true ? ClassListPayResultHistoryEnum::RESULT_SUCCESS : ClassListPayResultHistoryEnum::RESULT_FAILURE;
            } else {
                $vResult = ClassListPayResultHistoryEnum::RESULT_UNKNOW;
            }
            $arr = [
                /*推送时间*/
                'push_time'               => date('Y-m-d H:i:s'),
                'uniqid'                  => $val->uniqid,
                'pfs_class_num'           => $data['add_class_increment_total'],
                'pfs_dw_id'               => $data['pfs_dw_id'],
                'result'                  => $vResult,
                'result_desc'             => empty($result[$val->class_id]->desc) ? $errMsg : $result[$val->class_id]->desc,
                'org_class_id'            => $val->org_class_id,
                'class_id'                => $val->class_id,
                'class_name'              => $val->class_name,
                'stages_id'               => $val->stages_id,
                'stages_num'              => $val->stages_num,
                'ledger_subject_id'       => $val->ledger_subject_id,
                'ledger_subject_name'     => $val->ledger_subject_name,
                'area_code'               => $val->area_code,
                'city_name'               => $val->city_name,
                'enroll_class_hour'       => $val->enroll_class_hour,
                'pay_num'                 => $val->pay_num,
                'pay_amt'                 => $val->pay_amt,
                'pay_fee'                 => $val->pay_fee,
                'drop_class_num'          => $val->drop_class_num,
                'refund_num'              => $val->refund_num,
                'refund_amt'              => $val->refund_amt,
                'refund_fee'              => $val->refund_fee,
                'amount_today'            => $val->amount_today,
                'total_enroll_class_hour' => $val->total_enroll_class_hour,
                'total_drop_class_hour'   => $val->total_drop_class_hour,
                'total_pay_num'           => $val->total_pay_num,
                'total_pay_amt'           => $val->total_pay_amt,
                'total_pay_fee'           => $val->total_pay_fee,
                'total_refund_num'        => $val->total_refund_num,
                'total_refund_amt'        => $val->total_refund_amt,
                'total_refund_fee'        => $val->total_refund_fee,
                'transfer_flag'           => $val->transfer_flag,
                'statistic_date'          => $val->statistic_date,
                'create_date'             => $val->create_date,
            ];
            /*查询记录是否已经存在*/
            $exist = DB::select('SELECT `id` FROM `class_list_pay_result_history` WHERE `pfs_dw_id`=:pfs_dw_id AND `class_id`=:class_id AND `create_date`=:create_date', [
                'pfs_dw_id'   => $data['pfs_dw_id'],
                'class_id'    => $val->class_id,
                'create_date' => $val->create_date,
            ]);
            /*如果记录存在,就更新表记录*/
            if (!empty($exist)) {
                DB::table('class_list_pay_result_history')->where('id', $exist[0]->id)->update($arr);
                continue;
            }
            $list[] = $arr;
        }
        if (empty($list)) {
            return 0;
        }
        /*写入同步记录表*/
        return DB::table('class_list_pay_result_history')->insert($list);
    }

}





/**
 * PfsHeadAddclassincrementModel.php
 *
 * 说明:
 *
 * 修改历史
 * ----------------------------------------
 * 2021/6/7 16:13:00   操作:创建
 **/
